package edu.gatech.cc.CFAWaffleFriesWheresMyStuff.gui;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.R;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.Datastore;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.Datastore.UserDoesNotExistException;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.DatastoreFactory;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.Item;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.ItemManager;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.User;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.UserManager;

/**
 * Shows the form for adding a new item.
 * Transitions to the ItemList in all cases.
 * @author Matthew Barulic
 *
 */
public class AddItemActivity extends Activity {

	private User _user;
	private ItemManager _itemManager;
	private UserManager _userManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_item);
		Bundle extras = getIntent().getExtras();
		
		Datastore db = DatastoreFactory.getDatastore(getApplicationContext());
		
		_itemManager = new ItemManager(db);
		_userManager = new UserManager(db);
		
		try {
			_user = _userManager.getUser(extras.getString("user"));
		} catch (UserDoesNotExistException e) {
			e.printStackTrace();
		}
		
		final AddItemActivity me = this;
		
		((Button)findViewById(R.id.addItem_submit)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(me.addItem())
				{
					me.switchToMyItems();
				}
			}
		});
		
		((Button)findViewById(R.id.addItem_cancel)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				me.switchToMyItems();
			}
		});
	}

	/**
	 * Attempts to add an item.
	 * @return true if successful, false otherwise
	 */
	private boolean addItem()
	{
		String name = ((EditText)findViewById(R.id.addItem_itemName)).getText().toString();
		String description = ((EditText)findViewById(R.id.addItem_description)).getText().toString();
		int selectedType = ((Spinner)findViewById(R.id.addItem_itemTypeSpinner)).getSelectedItemPosition();
		String location = ((EditText)findViewById(R.id.addItem_location)).getText().toString();
		double reward = Double.parseDouble(((EditText)findViewById(R.id.addItem_reward)).getText().toString());
		int selectedCategory = ((Spinner)findViewById(R.id.addItem_itemCategorySpinner)).getSelectedItemPosition();
		
		if(name.isEmpty() || description.isEmpty() || location.isEmpty() || selectedType == Spinner.INVALID_POSITION)
		{
			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
			alertDialogBuilder.setTitle("Adding Item Failed");
			alertDialogBuilder.setMessage("Please complete all fields.");
			alertDialogBuilder.setCancelable(false);
			alertDialogBuilder.setNeutralButton("OK", null);
			AlertDialog alert = alertDialogBuilder.create();
			alert.show();
			return false;
		}
		
		Item.Type type;
		switch(selectedType)
		{
		case 0:
			type = Item.Type.Lost;
			break;
		case 1:
			type = Item.Type.Found;
			break;
		case 2:
			type = Item.Type.Donation;
			break;
		case 3:
			type = Item.Type.DonationRequest;
			break;
		default:
			type = Item.Type.Lost;
			break;
		}
		
		Item.Category category;
		switch(selectedCategory)
		{
		case 0:
			category = Item.Category.Keepsake;
			break;
		case 1:
			category = Item.Category.Heirloom;
			break;
		case 2:
			category = Item.Category.Miscellaneous;
			break;
		default:
			category = Item.Category.Keepsake;
			break;
		}
		
		_itemManager.createItem(_user.getUsername(), name, description, true, category, type, location, Calendar.getInstance().getTimeInMillis() / 1000, reward);
//		_user.createItem(name, description, category, type, null, reward);
		return true;
	}
	
	/**
	 * Switches to the ItemList activity displaying current
	 * user's items.
	 */
	private void switchToMyItems()
	{
		Intent i = new Intent(getApplicationContext(), ItemListActivity.class);
		i.putExtra("loggedIn", _user.getUsername());
		i.putExtra("user", _user.getUsername());
		i.putExtra("title", "My Items");
		startActivity(i);
		finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_add_item, menu);
		return true;
	}

}
